import * as Api from '../../api/api.js';
const app = getApp();
const os = app.os;
let scrollHeight = [0, 0];

Page({
  data: {
    newTitle: '',
    newsId: 3007,
    content: ''
  },
  
  onLoad: function (options) {
    let newsId = options?.id || 0;
    if(newsId === 0){
      newsId = this.data.newsId;
    }

    Api.getNewsDetail({news_id: newsId}).then(res => {
      if (res.code == 200) {
        os.setNavigationBarTitle({title: res.data.n_title});
        this.setData({content: res.data.n_content, newTitle: res.data.n_title});
      }
    });
  },

  onReady: function () {
    app.sendAppLogs('app', 3, {
      method_id: this.data.newsId
    }); 
  },

  onPageScroll: function (e) {
    scrollHeight[1] = parseInt(e.scrollTop);

    if (this.timeout) {
      clearTimeout(this.timeout);
    }
    
    this.timeout = setTimeout(() => {
      clearTimeout(this.timeout);

      if(scrollHeight[0] !== scrollHeight[1]){
        app.sendAppLogs('app', 5, {
          method_id: this.data.newsId,
          method_mark: scrollHeight[0],
          method_mark_1: scrollHeight[1],
        });
        scrollHeight[0] = scrollHeight[1];
      }

    }, 300);
  }
})